/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.controller.website_management;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.card.model.query.WechatUserQuery;
import top.continew.admin.card.model.req.UserWechatRelationReq;
import top.continew.admin.card.model.req.WechatUserReq;
import top.continew.admin.card.model.resp.WechatUserDetailResp;
import top.continew.admin.card.model.resp.WechatUserResp;
import top.continew.admin.card.service.UserWechatRelationService;
import top.continew.admin.card.service.WechatUserService;
import top.continew.admin.system.service.UserService;
import top.continew.starter.core.util.validate.CheckUtils;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.resp.BaseIdResp;
import top.continew.starter.extension.crud.util.ValidateGroup;
import top.continew.starter.log.core.annotation.Log;

/**
 * 微信用户管理 API
 *
 * @author floyd
 * @since 2024/12/02 15:34
 */
@Tag(name = "微信用户管理 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/card/wechatUser", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class WechatUserController extends BaseController<WechatUserService, WechatUserResp, WechatUserDetailResp, WechatUserQuery, WechatUserReq> {

    private final UserWechatRelationService userWechatRelationService;
    private final UserService userService;

    @Log(ignore = true)
    @Operation(summary = "保存微信用户信息", description = "保存微信用户信息")
    @PostMapping("/saveWechatUser")
    public Long saveUser(@Validated(ValidateGroup.Crud.Add.class) @RequestBody WechatUserReq req) {
        //BaseIdResp<Long> id = new
        return baseService.saveUser(req);
    }

    @Log(ignore = true)
    @Operation(summary = "快捷登录", description = "关联系统用户快捷登录")
    @PostMapping("/easyLogin")
    public BaseIdResp<Long> easyLogin(String phone, String openId) {
        BaseIdResp<Long> idResp = BaseIdResp.<Long>builder().build();

        var userModel = userService.getByPhone(phone);
        if (userModel == null) {
            CheckUtils.throwIf(true, "非系统用户无法进行此操作！");
        } else {
            UserWechatRelationReq req = new UserWechatRelationReq();
            req.setUserId(userModel.getId());
            req.setOpenId(openId);

            //根据openId判断是否已绑定用户
            var isBind = userWechatRelationService.isUserExists(openId);
            if (isBind) {
                CheckUtils.throwIf(!userWechatRelationService.isRelationExists(req), "提供的手机号码与绑定微信号不一致！");
            } else {
                //绑定用户
                userWechatRelationService.bindUser(req);
            }

            idResp.setId(userModel.getId());
        }
        return idResp;
    }
}